// __PUBLIC__/js/page/cms/block/addblock_content
require(['../../common'], function() {
  require(['block.addblock_content'])
})

define('block.addblock_content', [
  'jquery',
  'css!ztree/ztree.css',
  'ztree/ztree'
], function($) {
  'use strict';

  var setting = {
    view: {
      showLine: false
    },
    data: {
      simpleData: {
        enable: true
      }
    },
    callback: {
      beforeClick: beforeClick
    }
  };
  var setting2 = {
    edit: {
      enable: true,
      drag: {
        isCopy: false,
        isMove: true
      },
      showRemoveBtn: false,
      showRenameBtn: false
    },
    data: {
      simpleData: {
        enable: true
      }
    },
    callback: {
      beforeDrag: beforeDrag,
      beforeDrop: beforeDrop
    }
  };

  function beforeDrag(treeId, treeNodes) {
    for (var i = 0, l = treeNodes.length; i < l; i++) {
      if (treeNodes[i].drag === false) {
        return false;
      }
    }
    return true;
  }

  function beforeDrop(treeId, treeNodes, targetNode, moveType) {
    var treeObj = $.fn.zTree.getZTreeObj("treeDemo3");
    var nodes = treeObj.getNodes();
    if (targetNode) {
      return !((moveType != "prev" && !targetNode.parentTId));
    } else {
      return targetNode ? targetNode.drop !== false : true;
    }
  }

  function beforeClick(treeId, treeNode) {
    $("#showtree").show();
    $("#J_add").show();
    $.ajax({
      async: false,
      cache: false,
      type: 'POST',
      dataType: "json",
      data: "id=" + treeNode.id,
      url: APP + '?app=cms&m=Article&a=getArticleList',
      success: function(data) {
        $.fn.zTree.init($("#treeDemo2"), setting2, data);
      }
    });
  }
  $(document).ready(function() {
    $.ajax({
      async: false,
      cache: false,
      type: 'POST',
      dataType: "json",
      url: APP + '?app=cms&m=Site&a=getSiteCateInfo',
      success: function(data) {
        $.fn.zTree.init($("#treeDemo"), setting, data);
        $.fn.zTree.init($("#treeDemo3"), setting2);
      }
    });
  });

  function addTreeNodes(tree1, tree2) {
    var treeObj = $.fn.zTree.getZTreeObj(tree1);
    var nodes = treeObj.getSelectedNodes();

    var treeObj3 = $.fn.zTree.getZTreeObj(tree2);
    var newNodes = treeObj3.addNodes(null, nodes);
    for (var i = 0, l = nodes.length; i < l; i++) {
      treeObj.removeNode(nodes[i]);
    }
  }
  //获取所有选中节点的值

  function getCheckedAll() {
    var treeObj = $.fn.zTree.getZTreeObj("treeDemo3");
    var nodes = treeObj.getNodes();
    if (nodes.length <= 0) {
      alert('请选择要添加的内容')
    } else {
      var arr = []
      for (var i = 0; i < nodes.length; i++) {
        arr.push(nodes[i]['id'])
      }
      var block_id = $("#block_id").val()

      $.post(APP + '?app=cms&m=Block&a=createBlock', {
        arr: arr,
        block_id: block_id
      }).done(function(msg) {
        if (msg === '1') {
          alert('添加成功')
          location.href = APP + '?app=cms&m=Block&a=ajax_show&type=block&id=' + $('#block_id').val() + '&tid=' + $('#tid').val() + '&cid=' + $('#cid').val()
        } else {
          alert(msg)
        }
      })
    }
  }

  $(document)
  .on('click', '#J_moveLeft', function(e) {
    e.preventDefault()
    addTreeNodes('treeDemo2','treeDemo3')
  })
  .on('click', '#J_moveRight', function(e) {
  	e.preventDefault()
  	addTreeNodes('treeDemo3','treeDemo2')
  })
  .on('click', '#J_add', function(e) {
  	e.preventDefault()
  	getCheckedAll()
  })
})